home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 32 / Amiga Format AFCD32 (Nov 1998, Issue 117).iso / -seriously_amiga- / programming / other / hrtmon / src / fbuffers.s < prev    next >
Text File  |  1998-08-10  |  5KB  |  241 lines

  1.  
  2. ;HRTmon Amiga system monitor
  3. ;Copyright (C) 1991-1998 Alain Malek Alain.Malek@cryogen.com
  4. ;
  5. ;This program is free software; you can redistribute it and/or
  6. ;modify it under the terms of the GNU General Public License
  7. ;as published by the Free Software Foundation; either version 2
  8. ;of the License, or (at your option) any later version.
  9. ;
  10. ;This program is distributed in the hope that it will be useful,
  11. ;but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;GNU General Public License for more details.
  14. ;
  15. ;You can find the full GNU GPL online at: http://www.gnu.org
  16.  
  17. ;block caching routines
  18.  
  19. nb_fbuf equ 8    ;max nb of blocks cached
  20.  
  21. ;------------- file_buffers ----------------------------------------------
  22. ;------------- LRU replacement algorithm ---------------------------------
  23.  
  24. ;-> a3=partition
  25. ;-> d0=no of block to read
  26. ;-> a0=address of block
  27.  
  28. read_fbuffer    movem.l    d0-d1/a0-a3,-(a7)
  29.         st    disk_op
  30.  
  31.         bsr    inc_age
  32.  
  33.         move.l    #fbuffer1,d1
  34. .next        move.l    d1,a1
  35.         tst.l    fbuf_block(a1)        ;free ?
  36.         bmi.b    .nothis
  37.         cmp.l    fbuf_part(a1),a3    ;same partition ?
  38.         bne.b    .nothis
  39.         cmp.l    fbuf_block(a1),d0    ;same block no ?
  40.         bne.b    .nothis
  41.         clr.w    fbuf_age(a1)
  42.         move.l    fbuf_ptr(a1),a2
  43.         move.w    #512/8-1,d0
  44. .read        move.l    (a2)+,(a0)+
  45.         move.l    (a2)+,(a0)+
  46.         dbf    d0,.read
  47.         bra.b    .end
  48. .nothis        move.l    fbuf_next(a1),d1
  49.         bne.b    .next
  50.  
  51.         bsr    find_freefbuf
  52.  
  53.         move.l    a3,fbuf_part(a1)
  54.         move.l    d0,fbuf_block(a1)
  55.         sf    fbuf_written(a1)
  56.         clr.w    fbuf_age(a1)
  57.         move.l    part_device(a3),a2
  58.         jsr    READ_CMD(a2)        ;read block into dest
  59.         move.l    fbuf_ptr(a1),a2
  60.         move.w    #512/8-1,d0
  61. .copy        move.l    (a0)+,(a2)+        ;copy block into fbuf
  62.         move.l    (a0)+,(a2)+
  63.         dbf    d0,.copy
  64.  
  65. .end        movem.l    (a7)+,d0-d1/a0-a3
  66.         rts
  67.  
  68. inc_age        movem.l    d1/a1,-(a7)
  69.         move.l    #fbuffer1,d1
  70. .loop        move.l    d1,a1
  71.         tst.l    fbuf_block(a1)        ;block used ?
  72.         bmi.b    .free
  73.         cmp.w    #$7fff,fbuf_age(a1)    ;reached max val. ?
  74.         beq.b    .free
  75.         addq.w    #1,fbuf_age(a1)
  76. .free        move.l    fbuf_next(a1),d1
  77.         bne.b    .loop
  78.         movem.l    (a7)+,d1/a1
  79.         rts
  80.  
  81. ;<- a1=ptr on fbuffer (write back one if needed)
  82.  
  83. find_freefbuf    movem.l    d0-d2/a0/a2-a3,-(a7)
  84.         moveq    #-1,d0            ;to find max age
  85.         move.l    #fbuffer1,d1
  86. .loop        move.l    d1,a1
  87.         tst.l    fbuf_block(a1)
  88.         bmi.w    .found
  89.         cmp.w    fbuf_age(a1),d0        ;older ?
  90.         bgt.b    .notolder
  91.         blt.b    .go
  92.         tst.b    fbuf_written(a1);if same age => avoid to write-back
  93.         bne.b    .notolder
  94. .go        move.w    fbuf_age(a1),d0
  95.         move.l    a1,a0
  96. .notolder    move.l    fbuf_next(a1),d1
  97.         bne.b    .loop
  98.  
  99.         move.l    a0,a1    ;this is the fbuffer to kill (older)
  100.  
  101.         tst.b    fbuf_written(a1)    ;need to write-back ?
  102.         beq.b    .found
  103.  
  104.         move.l    fbuf_block(a1),d0
  105.         move.l    fbuf_part(a1),a3
  106.         moveq    #1,d1            ;nb blocks to writem
  107.         move.l    fbuf_ptr(a1),-(a7)    ;block address on stack
  108.         addq.l    #1,d0            ;seek next block
  109.         move.l    #fbuffer1,d2
  110. .loop2        move.l    d2,a2
  111.         tst.l    fbuf_block(a2)
  112.         bmi.b    .no
  113.         cmp.l    fbuf_block(a2),d0    ;next block ?
  114.         bne.b    .no
  115.         cmp.l    fbuf_part(a2),a3    ;same part ?
  116.         bne.b    .no
  117.         tst.b    fbuf_written(a2)    ;need to write ?
  118.         beq.b    .no
  119.         addq.l    #1,d1            ;write 1 more block
  120.         addq.l    #1,d0            ;seek next block
  121.         move.l    fbuf_ptr(a2),-(a7)
  122.         sf    fbuf_written(a2)
  123.         move.l    #fbuffer1,d2
  124.         bra.b    .loop2
  125. .no        move.l    fbuf_next(a2),d2
  126.         bne.b    .loop2
  127.  
  128.         sub.l    d1,d0
  129.         move.l    part_device(a3),a2
  130.         jsr    WRITEM_CMD(a2)        ;write back block(s)
  131.  
  132. .found        movem.l    (a7)+,d0-d2/a0/a2-a3
  133.         rts
  134.  
  135. ;-> a3=partition
  136. ;-> d0=no of block to write
  137. ;-> a0=address of block
  138.  
  139. write_fbuffer    movem.l    d0-d1/a0-a3,-(a7)
  140.         st    disk_op
  141.  
  142.         bsr    inc_age
  143.  
  144.         move.l    #fbuffer1,d1
  145. .next        move.l    d1,a1
  146.         tst.l    fbuf_block(a1)        ;free ?
  147.         bmi.b    .nothis
  148.         cmp.l    fbuf_part(a1),a3    ;same partition ?
  149.         bne.b    .nothis
  150.         cmp.l    fbuf_block(a1),d0    ;same block no ?
  151.         bne.b    .nothis
  152.         clr.w    fbuf_age(a1)
  153.         st    fbuf_written(a1)    ;need to write-back
  154.         move.l    fbuf_ptr(a1),a2
  155.         move.w    #512/8-1,d0
  156. .write        move.l    (a0)+,(a2)+
  157.         move.l    (a0)+,(a2)+
  158.         dbf    d0,.write
  159.         bra.b    .end
  160. .nothis        move.l    fbuf_next(a1),d1
  161.         bne.b    .next
  162.  
  163.         bsr    find_freefbuf
  164.  
  165.         move.l    a3,fbuf_part(a1)
  166.         move.l    d0,fbuf_block(a1)
  167.         st    fbuf_written(a1)
  168.         clr.w    fbuf_age(a1)
  169.         move.l    fbuf_ptr(a1),a2
  170.         move.w    #512/8-1,d0
  171. .copy        move.l    (a0)+,(a2)+        ;copy block into fbuf
  172.         move.l    (a0)+,(a2)+
  173.         dbf    d0,.copy
  174.  
  175. .end        movem.l    (a7)+,d0-d1/a0-a3
  176.         rts
  177.  
  178.  
  179. flush_fbuffer    movem.l    d0/a0-a3,-(a7)
  180.         move.l    #fbuffer1,d0
  181. .loop        move.l    d0,a1
  182.         tst.l    fbuf_block(a1)        ;block in buffer ?
  183.         bmi.b    .free
  184.         tst.b    fbuf_written(a1)    ;need to update ?
  185.         beq.b    .clear
  186.         move.l    fbuf_part(a1),a3
  187.         move.l    part_device(a3),a2
  188.         move.l    fbuf_ptr(a1),a0
  189.         move.l    fbuf_block(a1),d0
  190.         jsr    WRITE_CMD(a2)        ;write back to disk
  191.         jsr    UPDATE_CMD(a2)
  192. .clear        moveq    #-1,d0
  193.         move.l    d0,fbuf_block(a1)    ;set it to free
  194. .free        move.l    fbuf_next(a1),d0
  195.         bne.b    .loop
  196.         movem.l    (a7)+,d0/a0-a3
  197.         rts
  198.  
  199.  
  200. init_fbuffer    movem.l    d0/a0-a2,-(a7)
  201.         lea.l    fbuffer1,a0
  202.         lea.l    fbuf1,a1
  203.         move.w    #nb_fbuf-1,d0
  204.         move.l    a0,a2
  205. .loop        move.l    a0,(a2)
  206.         clr.l    (a0)
  207.         move.l    a1,fbuf_ptr(a0)
  208.         clr.l    fbuf_part(a0)
  209.         move.l    #-1,fbuf_block(a0)
  210.         clr.w    fbuf_age(a0)
  211.         sf    fbuf_written(a0)
  212.         move.l    a0,a2
  213.         lea.l    fbuf_SIZEOF(a0),a0
  214.         lea.l    512(a1),a1
  215.         dbf    d0,.loop
  216.         movem.l    (a7)+,d0/a0-a2
  217.         rts
  218.  
  219.         STRUCTURE fbuffers,0
  220.         ULONG fbuf_next
  221.         ULONG fbuf_ptr
  222.         ULONG fbuf_part
  223.         ULONG fbuf_block
  224.         UWORD fbuf_age
  225.         UBYTE fbuf_written
  226.         UBYTE fbuf_align
  227.         LABEL fbuf_SIZEOF
  228.  
  229. ;-------------- block buffer to speedup file access -------
  230.  
  231. fbuffer1    rept nb_fbuf
  232.         dc.l 0            ;next
  233.         dc.l 0            ;ptr
  234.         dc.l 0            ;part
  235.         dc.l -1            ;block no
  236.         dc.w 0            ;age
  237.         dc.b 0,0        ;written, align
  238.         endr
  239.  
  240.  
  241.